package days48;

import ThreadDemo.singal;

import java.util.LinkedList;

public class day32 {
    public static void main2(String[] args) throws InterruptedException {
        Thread c=new Thread(()->{
            System.out.println(Thread.currentThread().getName());
        },"c");
        Thread b=new Thread(()-> {
            try {
                c.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        },"b");
        Thread a=new Thread(()-> {
            try {
                b.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        },"a");
        a.start();
        b.start();
        c.start();
    }
public static void main1(String[] args) throws InterruptedException {
    // 创建三个线程
    Thread tc = new Thread(() -> {
        // 打印c
        System.out.print(Thread.currentThread().getName() + " ");
    }, "c");

    Thread tb = new Thread(() -> {
        try {
            // 等待c 执行完成
            tc.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 打印b
        System.out.print(Thread.currentThread().getName() + " ");
    }, "b");

    Thread ta = new Thread(() -> {
        try {
            // 等待b 执行完成
            tb.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 打印a
        System.out.print(Thread.currentThread().getName() + " ");
    }, "a");

    // 需要让他们同时启动，并按 c，b，a的顺序打印
    ta.start();
    tb.start();
    tc.start();
}
    private static Object locker1 = new Object();
    private static Object locker2 = new Object();
    private static Object locker3 = new Object();

    public static void main7(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker1) {
                        locker1.wait();
                    }
                    System.out.print("A");
                    synchronized (locker2) {
                        locker2.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker2) {
                        locker2.wait();
                    }
                    System.out.print("B");
                    synchronized (locker3) {
                        locker3.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker3) {
                        locker3.wait();
                    }
                    System.out.println("C");
                    synchronized (locker1) {
                        locker1.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);

        // 从线程 t1 启动
        synchronized (locker1) {
            locker1.notify();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread A=new Thread(()->{
            for (int i=0;i<10;i++) {
                synchronized (locker1){
                    try {
                        locker1.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print("A");
                synchronized (locker2){
                    locker2.notify();
                }
            }
        });
        Thread B=new Thread(()->{
            for (int i=0;i<10;i++) {
                synchronized (locker2){
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print("B");
                synchronized (locker3){
                    locker3.notify();
                }
            }
        });
        Thread C=new Thread(()->{
            for (int i=0;i<10;i++) {
                synchronized (locker3){
                    try {
                        locker3.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("C");
                synchronized (locker1){
                    locker1.notify();
                }
            }
        });
        A.start();
        B.start();
        C.start();
        Thread.sleep(1000);

        // 从线程 t1 启动
        synchronized (locker1) {
            locker1.notify();
        }
    }
    static class Singal{
        public Singal() {
        }
        private volatile static singal singal=null;

        public static void getSingal() {
            if(singal==null){
                synchronized (Singal.class){
                    if(singal==null){
                        singal=new singal();
                    }
                }
            }
        }
    }

}
